using UnityEngine;
using System.Collections;

public class CameraRail : MonoBehaviour {
	
	public GameObject currentPoint;
	public GameObject nextPoint;
	public GameObject orig;
	public GameObject target;
	
	private Vector3 tarToCam;
	private Vector3 tarToCurrent;
	private Vector3 nextToCurrent;
	
	public float interpolationSpeed;
	
	// Use this for initialization
	void Start () 
	{
		currentPoint = orig;
		transform.position = currentPoint.transform.position;
		nextPoint = currentPoint.GetComponent<CameraPoint>().getNext();
		
		tarToCam = target.transform.position - transform.position;	
	
		nextToCurrent = nextPoint.transform.position - currentPoint.transform.position;
	}
	
	// Update is called once per frame
	void Update () 
	{	
		tarToCam = target.transform.position - transform.position;
		//determiner la position de la camera sur la ligne current / next
		//deplacer la camera 
		SetPosition();
		//appliquer l'orientation 
		SetRotation();
	}
	
	void SetPosition()
	{
		tarToCurrent = target.transform.position - currentPoint.transform.position;
		
		
		// on projete le vecteur cur -> tar sur le vecteur cur->next
		Vector3 projected = Vector3.Project(tarToCurrent,nextToCurrent);
		
		// simplification pour calcul d'orientation
		Vector3 normNtC = Vector3.Normalize(nextToCurrent);
		//Vector3 normTtC = Vector3.Normalize(tarToCurrent);
		
		// on dot
		float dot = Vector3.Dot(normNtC,Vector3.Normalize(projected));
		
		Vector3 nextPos = currentPoint.transform.position + projected;
		
		// si le rapport <= 0 ( alors c est qu'on a dépassé le point courant )
		// 	il faut alors changer le prochain point pour aller vers previous
		//  s'il n'existe pas alors on clampe la position
	
		
		if(dot < 0)
		{
			Debug.Log("derrier");
			if(currentPoint.GetComponent<CameraPoint>().getPrevious() != null)
			{
				nextPoint = currentPoint.GetComponent<CameraPoint>().getPrevious();	
				nextToCurrent = nextPoint.transform.position - currentPoint.transform.position;
				
				nextPos = currentPoint.transform.position;
			}
			else
			{
				nextPos = currentPoint.transform.position;	
			}
		}
		
		// si le rapport >= 100 ( alors c 'est qu'on a dépassé le prochain point
		// il faut alors changer le prochain point pour aller vers next
		// s'il n'existe pas alors on bloque la position
		else if(dot > 0)
		{
			Debug.Log("devant");
			if(nextPoint != null)
			{
				Debug.Log("devant2");
				
				if(Vector3.Distance(currentPoint.transform.position,nextPos) >= Vector3.Distance(currentPoint.transform.position,nextPoint.transform.position))
				{	
					Debug.Log("devant3");
					currentPoint = nextPoint;
					nextPoint = currentPoint.GetComponent<CameraPoint>().getNext();
					nextToCurrent = nextPoint.transform.position - currentPoint.transform.position;
					
					nextPos = currentPoint.transform.position;
				}
				else
				{
					nextPos = currentPoint.transform.position + projected;		
				}
			}
			else
			{
				nextPos = currentPoint.transform.position;
			}
			
		}
		
		
		//Debug.Log("vector" + projected);
		//Debug.DrawLine(currentPoint.transform.position,currentPoint.transform.position + projected);	
		 
		
		
		transform.position = Vector3.MoveTowards(transform.position,nextPos,interpolationSpeed);
	}
	
	// apply final rotation to points the camera 
	void SetRotation()
	{
		tarToCam.Normalize();
		
		Quaternion newRotation = Quaternion.LookRotation(tarToCam);
		transform.rotation = newRotation;
	}
}
